package hotel.quartz.oversea.config.service.impl;

import com.alibaba.fastjson.JSONObject;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.JacksonUtil;
import hotel.base.oversea.utils.Result;
import hotel.base.oversea.vo.ResultStatusInfoVo;
import hotel.base.oversea.vo.switchs.BatchPushRoomVo;
import hotel.base.oversea.vo.switchs.RoomDataEntityVo;
import hotel.base.oversea.vo.switchs.RoomPriceModelVo;
import hotel.quartz.oversea.config.service.SwitchRoomPriceService;
import hotel.quartz.oversea.util.SwitchsHttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lmf
 * @Date 2020/6/2 15:11
 **/
@Service
public class SwitchRoomPriceServiceImpl implements SwitchRoomPriceService {
    Logger logger= LoggerFactory.getLogger(SwitchRoomPriceServiceImpl.class);
    @Value("${profile.type}")
    private String profile;

    /**
     * 直接推送报价等数据接口
     * @param batchPushRoomVo
     * @return
     */
    @Override
    public Result<Map<String,String>> batchPushRoomData(BatchPushRoomVo batchPushRoomVo) {

        logger.info("进入>SwitchRoomPriceServiceImpl>>>batchPushRoomData--方法");
        Map<String,Object> map=new HashMap<>();
        //获取酒店id
        int hotelId=batchPushRoomVo.getHotelId();
        if(hotelId==0){
            return new Result<>("酒店id不能为空");
        }
        map.put("hotelId",hotelId);

        int day=0;
        List<RoomDataEntityVo> roomDataEntityVos=batchPushRoomVo.getRoomDataEntitys();
        if(roomDataEntityVos.size()<1){
            return new Result<>("房型推送数据实体不能为空");
        }
        for (RoomDataEntityVo roomDataEntityVo:roomDataEntityVos){
            int roomId=roomDataEntityVo.getRoomId();
            if(roomId==0){
                return new Result<>("售卖房型Id不能为空");
            }
            String startDate=roomDataEntityVo.getStartDate();
            if(org.springframework.util.StringUtils.isEmpty(startDate)){
                return new Result<>("开始时间不能为空");
            }
            String endDate=roomDataEntityVo.getEndDate();
            if(org.springframework.util.StringUtils.isEmpty(endDate)){
                return new Result<>("结束时间不能为空");
            }
            day=day+ DateUtil.getIntervalDays(startDate,endDate);
            //房价推送实体
            RoomPriceModelVo roomPriceModelVo=roomDataEntityVo.getRoomPriceModel();
            if(!ObjectUtils.isEmpty(roomPriceModelVo)){
                double roomPrice=roomPriceModelVo.getRoomPrice();
                if(roomPrice==0){
                    return new Result<>("房价不能为空");
                }
                String channel=roomPriceModelVo.getChannel();
                if(org.springframework.util.StringUtils.isEmpty(channel)){
                    return new Result<>("渠道不能为空");
                }
            }
            //采购价推送实体
         /*   PurchasePriceModelVo purchasePriceModelVo=roomDataEntityVo.getPurchasePriceModel();
            if(!ObjectUtils.isEmpty(purchasePriceModelVo)){
                double roomPrice=purchasePriceModelVo.getPurchasePrice();
                if(roomPrice==0){
                    return new Result<>("房价不能为空");
                }
            }*/
            //动态售卖规则实体 暂时不需要设置因为创建已经设置过
            /*SaleRuleModelVo saleRuleModelVo=roomDataEntityVo.getSaleRuleModel();
            if(!StringUtils.isEmpty(saleRuleModelVo.getChannel())){
                if(!ObjectUtils.isEmpty(saleRuleModelVo.getSellingRule())){
                    SellingRuleVo sellingRuleVo=saleRuleModelVo.getSellingRule();
                    if(sellingRuleVo.getLatestconfirmTimeOfHours()==0){
                        return new Result<>("最晚立即确认时间-小时部分设置非法roomId="+roomId+",startDate="+startDate+",endDate="+endDate+",channel="+saleRuleModelVo.getChannel()+"最晚立即确认时间-小时部分0");
                    }
                }
                if(!ObjectUtils.isEmpty(saleRuleModelVo.getCtripSellRule())){
                    CtripSellRuleVo ctripSellRuleVo=saleRuleModelVo.getCtripSellRule();
                    if(ctripSellRuleVo.getLatestconfirmTimeOfHours()==0){
                        return new Result<>("ctri最晚立即确认时间-小时部分设置非法roomId="+roomId+",startDate="+startDate+",endDate="+endDate+",channel="+saleRuleModelVo.getChannel()+"最晚立即确认时间-小时部分0");
                    }
                    if(ctripSellRuleVo.getLatestBookingTimeOfHours()==0){
                        return new Result<>("ctri最晚预定时间-小时部分设置非法roomId="+roomId+",startDate="+startDate+",endDate="+endDate+",channel="+saleRuleModelVo.getChannel()+"最晚立即确认时间-小时部分0");
                    }
                    if(ctripSellRuleVo.getLatestCancelTimeOfHours()==0){
                        return new Result<>("ctri最晚取消时间-小时部分设置非法roomId="+roomId+",startDate="+startDate+",endDate="+endDate+",channel="+saleRuleModelVo.getChannel()+"最晚立即确认时间-小时部分0");
                    }
                }
            }*/
        }
        //多个房型涉及的天数不能大于300（房型个数*天数）
        if(day*roomDataEntityVos.size()>300){
            return new Result<>("多个房型涉及的天数不能大于300（房型个数*天数）");
        }
        map.put("roomDataEntitys",roomDataEntityVos);
        logger.info("直接推送报价等数据接口");
        String result= SwitchsHttpUtils.sendRequest(map,"BatchPushRoomData",profile);
        //对结构进行格式化
        JSONObject jsonObject = JSONObject.parseObject(result);
        System.out.println(result);
        ResultStatusInfoVo resultStatusInfoVo= JacksonUtil.parse(jsonObject.getString("resultStatus"), ResultStatusInfoVo.class);
        if(resultStatusInfoVo.getResultCode()!=0){
            return new Result<>(resultStatusInfoVo.getResultMsg(), CommonConstants.REQUEST_ERROR);
        }
        String logKey=jsonObject.getString("logKey");
        Map<String,String> resultData=new HashMap<>();
        resultData.put("logKey",logKey);
        return new Result<>(resultData);
    }


}
